﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TodayWhoModel
{
    public class UserDisplayModel
    {
        #region fields
        private int userId;

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        private bool isActive;

        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }

        private bool isControlled;

        public bool IsControlled
        {
            get { return isControlled; }
            set { isControlled = value; }
        }

        private bool isAppropriate;

        public bool IsAppropriate
        {
            get { return isAppropriate; }
            set { isAppropriate = value; }
        }
        private bool isDisplayed;

        public bool IsDisplayed
        {
            get { return isDisplayed; }
            set { isDisplayed = value; }
        }
        private DateTime displayDate;

        public DateTime DisplayDate
        {
            get { return displayDate; }
            set { displayDate = value; }
        }
        #endregion

        #region db methods

        public bool Insert()
        {
            string ssql = "INSERT INTO [User-Displays] (userId) VALUES (@userId)";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        public bool UpdateIsActive()
        {
            string ssql = "UPDATE [User-Displays]  SET isActive=@isActive WHERE userId=@userId";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@isActive", IsActive);

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        public bool UpdateIsAppropriate()
        {
            string ssql = "UPDATE [User-Displays]  SET isAppropriate=@isAppropriate WHERE userId=@userId";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@isAppropriate", IsAppropriate);

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        public bool UpdateIsControlled()
        {
            string ssql = "UPDATE [User-Displays]  SET isControlled=@isControlled WHERE userId=@userId";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@isControlled", IsControlled);

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        public bool SetDateAutomatically()
        {
            string ssql = @"DECLARE @lastDate datetime;
                            SET @lastDate = (SELECT TOP 1 UD.displayDate FROM [Users] as U INNER JOIN
	                            [Categories] as C ON U.categoryId=C.categoryId INNER JOIN
		                            [User-Displays] as UD ON U.userId=UD.userId
			                            WHERE U.categoryId= 
				                            (SELECT categoryId FROM [Users] WHERE userId=@userId)
			                             ORDER BY UD.displayDate DESC)
                            UPDATE [User-Displays] SET displayDate=DATEADD(day,1,@lastDate) WHERE userId=@userId";
            
            SqlCommand cmd = new SqlCommand(ssql);
            cmd.Parameters.AddWithValue("@userId", UserId);
            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }


        #endregion
    }
}
